﻿using CMS.Models;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class InputCargoViewModel: ViewModelBase
    {
      

        public AppData AppData { get; set; } = AppData.Instance;
        private Record record = new Record();
        /// <summary>
        /// 当前物资
        /// </summary>
        public Cardgo Cargo { get; set; }
        /// <summary>
        /// 入库流水单
        /// </summary>
        public Record Record

        {
            get { return record; }
            set { record = value; RaisePropertyChanged(nameof(Record)); }
        }
        private List<Cardgo> cargos = new List<Cardgo>();
        /// <summary>
        /// 关闭窗体
        /// </summary>
        public RelayCommand<Window> AddInputCloseCommand
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    if (arg == null) return;
                    arg.Close();
                });
            }
        }
        /// <summary>
        /// 添加新流水入库
        /// </summary>
        public RelayCommand<Window> AddRecordCommand
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                  //  if (string.IsNullOrEmpty(record.CargoName)) return;
                  //var cargo=this.Cargos.FirstOrDefault(item=>item.Name==record.CargoName);
                    if (Cargo == null) return;
                    record.CargoId= Cargo.Id;
                    record.CargoName= Cargo.Name;
                    if (record.Number==0) return;
                    record.InserDate= DateTime.Now;
                    record.MenberId= AppData.CurrentUser.Id;
                   record.MenberName=AppData.CurrentUser.Name;
                    record.RecoredType = true;//表示入库
                    var count = new RecordProvider().Insert(record);
                    if (count == 0)
                    {
                        MessageBox.Show("添加失败");
                    }
                    else
                    {
                        arg.Close();
                    }
                });
            }
        }
    }
}
